/**
 * @author dnk2
 */

function initNav() {
	var name = readCookie("koru_user") ;
	
	if (name==null || name=="") {
		document.getElementById("navRegister").style.display = "inline" ;
		document.getElementById("navScores").style.display = "none" ;
		document.getElementById("navTasks").style.display = "none" ;
	} else {
		document.getElementById("navRegister").style.display = "none" ;
		document.getElementById("navScores").style.display = "inline" ;
		document.getElementById("navTasks").style.display = "inline" ;
		
		document.getElementById("loginNav").style.display = "block" ;
		var usernameNav = document.getElementById("usernameNav") ;
		usernameNav.innerHTML = name ;
	}
	
	showNav() ;	
}

function showNav() {
	document.getElementById("navigation").style.display = "block" ;
	
	var name = readCookie("koru_user") ;
	if (name!=null && name!="") {
		document.getElementById("loginNav").style.display = "block" ;
		var usernameNav = document.getElementById("usernameNav") ;
		usernameNav.innerHTML = name ;
	}
}


function initPage() {
	var name = readCookie("koru_user") ;
	
	if (name==null || name=="") {
		document.getElementById("playerBox").style.display = "none" ;
		document.getElementById("loginBox").style.display = "block" ;
	} else {
		document.getElementById("navRegister").style.display = "none" ;
		document.getElementById("playerBox").style.display = "none" ;
		document.getElementById("loginBox").style.display = "none" ;
	
		requestPlayerDetails(name) ;
	}
	
	initNav() ;
}


//login ------------------------------------------------------------------------------------

function login() {
		
	var name = document.getElementById("login_name").value ;
	var pswd = document.getElementById("login_password").value ;
	var status = document.getElementById("login_message") ;
	
	status.style.display = "none" ;
	
	if (name==null|| name=="") {
		status.innerHTML = "You must specifiy your username" ;
		status.style.display = "inline" ;
		return ;		
	}
	
	if (pswd==null|| pswd=="") {
		status.innerHTML = "You must specifiy a password" ;
		status.style.display = "inline" ;
		return ;
	}
	
	var parName = newElementAtt('param', '', new Array('name', 'value'), new Array('name', name) ) ;
	var parPswd = newElementAtt('param', '', new Array('name', 'value'), new Array('password', pswd) ) ;
	
	var paramList = newElement('paramList', parName + parPswd);
  		 
  	var request = newElementAtt('request', paramList, new Array('lang', 'to', 'type', 'uid'), new Array('en',collection + '/Login', 'process', '1'  ) );
  	var message = newElement('message', request );
  	var soapMessage = messageToSOAP(message);
   
  	xmlHttp = XmlHttp.create();
  	xmlHttp.open("POST", "http://" + serverPath + "/services/localsite", true);
  	xmlHttp.onreadystatechange = new Function("processLoginResponse() ; ") ;
   
	xmlHttp.setRequestHeader("SOAPAction", "\"\"");
  	xmlHttp.setRequestHeader("Content-Type", "Content-Type: text/xml; charset=utf-8");
   
   	logDebug("Request: " + message) ;
   
  	xmlHttp.send(soapMessage);
}

function processLoginResponse() {
	
	if (this.xmlHttp.readyState == 4) {
		logDebug("Response: " + this.xmlHttp.responseXML.xml) ;
		alert(this.xmlHttp.responseXML.xml) ;		

		var accept = this.xmlHttp.responseXML.getElementsByTagName('accept')[0];
   
   		if (accept != null) {
			var name = document.getElementById("login_name").value ;
			createCookie("koru_user", name, 1) ;
			initPage() ;
		} else {
			var message = document.getElementById("login_message") ;
			message.innerHTML = this.xmlHttp.responseXML.xml ;
			message.style.display = "inline" ;
		}
	}
}

function logout() {
	eraseCookie("koru_user") ;
	window.location.href="index.htm" ;
}


//player details----------------------------------------------------------------------------

function requestPlayerDetails(name) {

	var parName = newElementAtt('param', '', new Array('name', 'value'), new Array('name', name) ) ;
		
	var paramList = newElement('paramList', parName);
  		 
  	var request = newElementAtt('request', paramList, new Array('lang', 'to', 'type', 'uid'), new Array('en',collection + '/GetPlayerDetails', 'process', '1'  ) );
  	var message = newElement('message', request );
  	var soapMessage = messageToSOAP(message);
   
  	xmlHttp = XmlHttp.create();
  	xmlHttp.open("POST", "http://" + serverPath + "/services/localsite", true);
  	xmlHttp.onreadystatechange = new Function("processPlayerDetailsResponse() ; ") ;
   
	xmlHttp.setRequestHeader("SOAPAction", "\"\"");
  	xmlHttp.setRequestHeader("Content-Type", "Content-Type: text/xml; charset=utf-8");
   
   	logDebug("Request: " + message) ;
   
  	xmlHttp.send(soapMessage);
}

function processPlayerDetailsResponse() {
	if (this.xmlHttp.readyState == 4) {
		logDebug("Details Response: " + this.xmlHttp.responseXML.xml) ;
		
		var player = this.xmlHttp.responseXML.getElementsByTagName('player')[0];
   
   
   
   		if (player != null) {
			
			var message ;
			
			if (Number(player.getAttribute("tasksCompleted")) > 0) {
					
				var rankChange = Number(player.getAttribute("rankChange")) ;
				var rc ;
				
				if (rankChange > 0) 
					rc =  "up <em>" + rankChange + "</em> places" ;
					
				if (rankChange < 0)
					rc = "down <em>" + (-1 * rankChange) + "</em> places" ;
					
				if (rankChange == 0)
					rc = "no change" ;
					
				rc = rc + " since " + player.getAttribute("lastRankCheck") ;
								
				message = "<p> " + 
					"Welcome back <em>" + player.getAttribute("name") + "</em> " +
					"</p>" + 
					"<p style=\"font-size:50px\"> <em>" + player.getAttribute("ranking") + "</em> </p>" +
					"<p>of <em>48</em> players </p>" + 
					"<p>" + rc + "</p>" + 
					"<p>" +	
					"Would you like to <a href=\"tasks.htm\">try another task?</a>" + 
					"</p>" ;
			} else {
				message = "<p> " + 
					"Welcome <em>" + player.getAttribute("name") + "</em> " +
					"</p>" + 
					"<p> You need to start on some <a href=\"tasks.htm\">tasks</a> and get yourself onto the scoreboard! </p>" ;
			}
			
			document.getElementById("playerText").innerHTML = message ; ;
			document.getElementById("playerBox").style.display = "block" ;
		} else {
			//var message = document.getElementById("login_message") ;
			//message.innerHTML = this.xmlHttp.responseXML.xml ;
			//message.style.display = "inline" ;
		}
	}
}





